package cn.geminis.data.jpa.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Entity
@Getter
@Setter
public class Clazz {

    @Id
    @Column(length = 36)
    private String id;

    @Column(nullable = false, length = 256)
    private String name;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "classes")
    //设置mappedBy，避免生成2张中间表
    private Set<Student> students = new HashSet<>();

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Teacher teacher;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "clazz")
    //指定mappedBy，避免两张表都生成外键
    private ClassAdviser adviser;

    @PrePersist
    void createId() {
        if (!StringUtils.hasText(this.id)) {
            this.id = UUID.randomUUID().toString();
        }
    }
}
